]> git.saurik.com Git - bison.git/blame - data/c++.m4
muscle: enforce definition syntax for keyword variables
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
7d6bad19 5# Copyright (C) 2002-2013 Free Software Foundation, Inc.
cf147260 6
f16b0819 7# This program is free software: you can redistribute it and/or modify
cf147260 8# it under the terms of the GNU General Public License as published by
f16b0819 9# the Free Software Foundation, either version 3 of the License, or
cf147260 10# (at your option) any later version.
f16b0819 11#
cf147260
AD
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
f16b0819 16#
cf147260 17# You should have received a copy of the GNU General Public License
f16b0819 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
cf147260 19
3953ed88
AD
20m4_include(b4_pkgdatadir/[c.m4])
21
38de4e57
AD
22# b4_comment(TEXT, [PREFIX])
23# --------------------------
24# Put TEXT in comment. Prefix all the output lines with PREFIX.
25m4_define([b4_comment],
26[b4_comment_([$1], [$2// ], [$2// ])])
27
07e65a77
AD
28## -------- ##
29## Checks. ##
30## -------- ##
31
32b4_percent_define_check_kind([api.namespace], [code], [deprecated])
38de4e57 33
cf147260
AD
34## ---------------- ##
35## Default values. ##
36## ---------------- ##
37
a4e25e1d 38b4_percent_define_default([[parser_class_name]], [[parser]])
7789b6e3
AD
39
40# Don't do that so that we remember whether we're using a user
41# request, or the default value.
42#
db8ab2be 43# b4_percent_define_default([[api.location.type]], [[location]])
7789b6e3 44
a4e25e1d 45b4_percent_define_default([[filename_type]], [[std::string]])
07e65a77 46# Make it a warning for those who used betas of Bison 3.0.
171ad99d 47b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 48
c1d19e10 49b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
50b4_percent_define_default([[define_location_comparison]],
51 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 52 [std::string], [[true]], [[false]])])
cf147260
AD
53
54
67501061 55
793fbca5
JD
56## ----------- ##
57## Namespace. ##
58## ----------- ##
59
67501061
AD
60m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
61
793fbca5 62
45eebca4
AD
63# Don't permit an empty b4_namespace_ref. Any '::parser::foo' appended to it
64# would compile as an absolute reference with 'parser' in the global namespace.
793fbca5
JD
65# b4_namespace_open would open an anonymous namespace and thus establish
66# internal linkage. This would compile. However, it's cryptic, and internal
67# linkage for the parser would be specified in all translation units that
68# include the header, which is always generated. If we ever need to permit
69# internal linkage somehow, surely we can find a cleaner approach.
70m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 71[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
72 [[namespace reference is empty]])])
73
74# Instead of assuming the C++ compiler will do it, Bison should reject any
75# invalid b4_namepsace_ref that would be converted to a valid
76# b4_namespace_open. The problem is that Bison doesn't always output
77# b4_namespace_ref to uncommented code but should reserve the ability to do so
78# in future releases without risking breaking any existing user grammars.
79# Specifically, don't allow empty names as b4_namespace_open would just convert
80# those into anonymous namespaces, and that might tempt some users.
81m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 82[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
83 [[namespace reference has consecutive "::"]])])
84m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 85[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
86 [[namespace reference has a trailing "::"]])])
87
88m4_define([b4_namespace_open],
67501061 89[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
90[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
91 [^\(.\)[ ]*::], [\1])),
92 [::], [ { namespace ])[ {]])])
93
94m4_define([b4_namespace_close],
67501061 95[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 96m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 97 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 98 [\1])),
b987342b 99 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
100
101
e3990e3c
AD
102# b4_token_enums
103# --------------
c095d689 104# Output the definition of the tokens as enums.
cf147260 105m4_define([b4_token_enums],
e3990e3c
AD
106[[enum yytokentype
107 {
108 ]m4_join([,
109 ],
110 b4_symbol_map([b4_token_enum]))[
111 };]dnl
c095d689 112])
cf147260
AD
113
114
ef51bfa7
AD
115
116
cf147260
AD
117## ----------------- ##
118## Semantic Values. ##
119## ----------------- ##
120
7601a471
AD
121
122
123# b4_value_type_declare
124# ---------------------
b9e4eb5b 125# Declare semantic_type.
7601a471 126m4_define([b4_value_type_declare],
6574576c 127[b4_value_type_setup[]dnl
b9e4eb5b 128[ /// Symbol semantic values.
435575cb
AD
129]m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
130[code],
131[[ typedef ]b4_percent_define_get([[api.value.type]])[ semantic_type;]],
132[m4_bmatch(b4_percent_define_get([[api.value.type]]),
133[union\|union-directive],
6574576c 134[[ union semantic_type
d2e3c807 135 {
435575cb
AD
136 ]b4_user_union_members[
137 };]])])dnl
138])
b9e4eb5b
AD
139
140
4f84717d
AD
141# b4_public_types_declare
142# -----------------------
143# Define the public types: token, semantic value, location, and so forth.
1d6b689b 144# Depending on %define token_lex, may be output in the header or source file.
4f84717d 145m4_define([b4_public_types_declare],
5458913a 146[[#ifndef ]b4_api_PREFIX[STYPE
7601a471 147]b4_value_type_declare[
4f84717d 148#else
5458913a 149 typedef ]b4_api_PREFIX[STYPE semantic_type;
4f84717d
AD
150#endif]b4_locations_if([
151 /// Symbol locations.
f6b561d9 152 typedef b4_percent_define_get([[api.location.type]],
7789b6e3 153 [[location]]) location_type;])[
1d6b689b 154
ff601366
AD
155 /// Syntax errors thrown from user actions.
156 struct syntax_error : std::runtime_error
157 {
158 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
159 location_type location;])[
160 };
161
4f84717d
AD
162 /// Tokens.
163 struct token
164 {
e3990e3c 165 ]b4_token_enums[
4f84717d 166 };
1d6b689b 167
26f95f5f 168 /// (External) token type, as returned by yylex.
4f84717d 169 typedef token::yytokentype token_type;
1d6b689b 170
26f95f5f
AD
171 /// Internal symbol number.
172 typedef int symbol_number_type;
173
174 /// Internal symbol number for tokens (subsumed by symbol_number_type).
175 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
176
1dbaf37f
TR
177 /// A complete symbol.
178 ///
179 /// Expects its Base type to provide access to the symbol type
180 /// via type_get().
181 ///
182 /// Provide access to semantic value]b4_locations_if([ and location])[.
183 template <typename Base>
184 struct basic_symbol : Base
1d6b689b 185 {
97ae878e
AD
186 /// Alias to Base.
187 typedef Base super_type;
188
1d6b689b 189 /// Default constructor.
b20e797a 190 basic_symbol ();
1d6b689b 191
1dbaf37f 192 /// Copy constructor.
b20e797a 193 basic_symbol (const basic_symbol& other);
ee9cf8c4
TR
194]b4_variant_if([[
195 /// Constructor for valueless symbols, and symbols from each type.
196]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
1dbaf37f 197 /// Constructor for valueless symbols.
7fc7df7a 198 basic_symbol (typename Base::kind_type t]b4_locations_if([,
ee9cf8c4 199 const location_type& l])[);]])[
1d6b689b 200
1dbaf37f 201 /// Constructor for symbols with semantic value.
7fc7df7a 202 basic_symbol (typename Base::kind_type t,
b20e797a
TR
203 const semantic_type& v]b4_locations_if([,
204 const location_type& l])[);
1d6b689b 205
97ae878e 206 ~basic_symbol ();
04816a6f 207
5f87211c 208 /// Destructive move, \a s is emptied into this.
b20e797a 209 void move (basic_symbol& s);
1d6b689b
AD
210
211 /// The semantic value.
212 semantic_type value;]b4_locations_if([
213
214 /// The location.
215 location_type location;])[
7d1aa2d6
AD
216
217 private:
218 /// Assignment operator.
219 basic_symbol& operator= (const basic_symbol& other);
1d6b689b
AD
220 };
221
1dbaf37f
TR
222 /// Type access provider for token (enum) based symbols.
223 struct by_type
1d6b689b 224 {
1d6b689b 225 /// Default constructor.
b20e797a 226 by_type ();
1d6b689b 227
1dbaf37f 228 /// Copy constructor.
b20e797a 229 by_type (const by_type& other);
1d6b689b 230
26f95f5f
AD
231 /// The symbol type as needed by the constructor.
232 typedef token_type kind_type;
1d6b689b 233
26f95f5f
AD
234 /// Constructor from (external) token numbers.
235 by_type (kind_type t);
97ae878e 236
26f95f5f
AD
237 /// Steal the symbol type from \a that.
238 void move (by_type& that);
1d6b689b 239
26f95f5f 240 /// The (internal) type number (corresponding to \a type).
97ae878e 241 /// -1 when this symbol is empty.
26f95f5f 242 symbol_number_type type_get () const;
1d6b689b 243
2b08bceb 244 /// The token.
b20e797a 245 token_type token () const;
1dbaf37f 246
675d9fe4
AD
247 enum { empty = 0 };
248
26f95f5f 249 /// The symbol type.
26f95f5f
AD
250 /// -1 when this symbol is empty.
251 token_number_type type;
1d6b689b 252 };
1dbaf37f
TR
253
254 /// "External" symbols: returned by the scanner.
255 typedef basic_symbol<by_type> symbol_type;
256
0623bacc 257]b4_symbol_constructor_declare])
4f84717d
AD
258
259
1d6b689b
AD
260# b4_public_types_define
261# ----------------------
262# Provide the implementation needed by the public types.
263m4_define([b4_public_types_define],
403febca 264[[ inline
ff601366
AD
265 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
266 : std::runtime_error (m)]b4_locations_if([
267 , location (l)])[
268 {}
269
1dbaf37f
TR
270 // basic_symbol.
271 template <typename Base>
b20e797a 272 inline
1dbaf37f
TR
273 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
274 : value ()
275 {}
04816a6f 276
1dbaf37f 277 template <typename Base>
b20e797a 278 inline
1dbaf37f
TR
279 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
280 : Base (other)
281 , value ()]b4_locations_if([
282 , location (other.location)])[
1d6b689b 283 {
1dbaf37f
TR
284 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
285 [other.value])],
286 [value = other.value;])[
1d6b689b
AD
287 }
288
1d6b689b 289
1dbaf37f 290 template <typename Base>
b20e797a 291 inline
1dbaf37f 292 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
7fc7df7a 293 [typename Base::kind_type t],
1dbaf37f
TR
294 [const semantic_type& v],
295 b4_locations_if([const location_type& l]))[)
296 : Base (t)
97ae878e 297 , value (]b4_variant_if([], [v])[)]b4_locations_if([
1dbaf37f 298 , location (l)])[
97ae878e 299 {]b4_variant_if([[
733fb7c5 300 (void) v;
97ae878e 301 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
1d6b689b 302
ee9cf8c4
TR
303]b4_variant_if([[
304 // Implementation of basic_symbol constructor for each type.
305]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
306 /// Constructor for valueless symbols.
1dbaf37f 307 template <typename Base>
b20e797a 308 inline
1dbaf37f 309 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
7fc7df7a 310 [typename Base::kind_type t],
1dbaf37f 311 b4_locations_if([const location_type& l]))[)
97ae878e
AD
312 : Base (t)
313 , value ()]b4_locations_if([
1dbaf37f 314 , location (l)])[
ee9cf8c4 315 {}]])[
1d6b689b 316
97ae878e
AD
317 template <typename Base>
318 inline
319 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
320 {]b4_variant_if([[
321 // User destructor.
26f95f5f 322 symbol_number_type yytype = this->type_get ();
97ae878e
AD
323 switch (yytype)
324 {
325]b4_symbol_foreach([b4_symbol_destructor])dnl
326[ default:
327 break;
328 }
329
330 // Type destructor.
331 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
332 }
333
1dbaf37f 334 template <typename Base>
b20e797a 335 inline
1dbaf37f
TR
336 void
337 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
1d6b689b 338 {
97ae878e
AD
339 super_type::move(s);
340 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
1dbaf37f 341 [s.value])],
97ae878e
AD
342 [value = s.value;])[]b4_locations_if([
343 location = s.location;])[
1d6b689b
AD
344 }
345
1dbaf37f 346 // by_type.
b20e797a 347 inline
1dbaf37f 348 ]b4_parser_class_name[::by_type::by_type ()
675d9fe4 349 : type (empty)
1dbaf37f 350 {}
1d6b689b 351
b20e797a 352 inline
1dbaf37f
TR
353 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
354 : type (other.type)
355 {}
1d6b689b 356
b20e797a 357 inline
1dbaf37f
TR
358 ]b4_parser_class_name[::by_type::by_type (token_type t)
359 : type (yytranslate_ (t))
360 {}
7be08dfb 361
97ae878e
AD
362 inline
363 void
364 ]b4_parser_class_name[::by_type::move (by_type& that)
365 {
366 type = that.type;
675d9fe4 367 that.type = empty;
97ae878e
AD
368 }
369
b20e797a 370 inline
1d6b689b 371 int
1dbaf37f 372 ]b4_parser_class_name[::by_type::type_get () const
1d6b689b
AD
373 {
374 return type;
375 }
e36ec1f4 376]b4_token_ctor_if([[
b20e797a 377 inline
1d6b689b 378 ]b4_parser_class_name[::token_type
1dbaf37f 379 ]b4_parser_class_name[::by_type::token () const
1d6b689b
AD
380 {
381 // YYTOKNUM[NUM] -- (External) token number corresponding to the
382 // (internal) symbol number NUM (which must be that of a token). */
383 static
384 const ]b4_int_type_for([b4_toknum])[
385 yytoken_number_[] =
386 {
387 ]b4_toknum[
388 };
389 return static_cast<token_type> (yytoken_number_[type]);
390 }
0623bacc
AD
391]])[]dnl
392b4_symbol_constructor_define])
393
394
395# b4_symbol_constructor_declare
396# b4_symbol_constructor_define
397# -----------------------------
398# Declare/define symbol constructors for all the value types.
399# Use at class-level. Redefined in variant.hh.
400m4_define([b4_symbol_constructor_declare], [])
401m4_define([b4_symbol_constructor_define], [])
402
1d6b689b 403
cbf25ce7
AD
404# b4_yytranslate_define
405# ---------------------
406# Define yytranslate_. Sometimes used in the header file,
407# sometimes in the cc file.
408m4_define([b4_yytranslate_define],
409[[ // Symbol number corresponding to token number t.
b20e797a 410 inline
cbf25ce7 411 ]b4_parser_class_name[::token_number_type
e36ec1f4 412 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
cbf25ce7
AD
413 [int])[ t)
414 {
415 static
416 const token_number_type
417 translate_table[] =
418 {
419]b4_translate[
420 };
421 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
422 const token_number_type undef_token_ = ]b4_undef_token_number[;
423
424 if (static_cast<int>(t) <= yyeof_)
425 return yyeof_;
426 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
427 return translate_table[t];
428 else
429 return undef_token_;
430 }
431]])
432
1d6b689b 433
cf147260
AD
434# b4_lhs_value([TYPE])
435# --------------------
436# Expansion of $<TYPE>$.
437m4_define([b4_lhs_value],
1fa5d8bb 438[b4_symbol_value([yyval], [$1])])
cf147260
AD
439
440
441# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
442# --------------------------------------
443# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
444# symbols on RHS.
445m4_define([b4_rhs_value],
1fa5d8bb
AD
446[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
447
cf147260
AD
448
449# b4_lhs_location()
450# -----------------
451# Expansion of @$.
452m4_define([b4_lhs_location],
453[(yyloc)])
454
455
456# b4_rhs_location(RULE-LENGTH, NUM)
457# ---------------------------------
458# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
459# on RHS.
460m4_define([b4_rhs_location],
d1ff7a7c 461[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
462
463
464# b4_parse_param_decl
465# -------------------
466# Extra formal arguments of the constructor.
467# Change the parameter names from "foo" into "foo_yyarg", so that
468# there is no collision bw the user chosen attribute name, and the
469# argument name in the constructor.
470m4_define([b4_parse_param_decl],
471[m4_ifset([b4_parse_param],
472 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
473
474m4_define([b4_parse_param_decl_1],
475[$1_yyarg])
476
477
478
479# b4_parse_param_cons
480# -------------------
481# Extra initialisations of the constructor.
482m4_define([b4_parse_param_cons],
483 [m4_ifset([b4_parse_param],
e9690142 484 [
cf147260
AD
485 b4_cc_constructor_calls(b4_parse_param)])])
486m4_define([b4_cc_constructor_calls],
e9690142 487 [m4_map_sep([b4_cc_constructor_call], [,
cf147260
AD
488 ], [$@])])
489m4_define([b4_cc_constructor_call],
e9690142 490 [$2 ($2_yyarg)])
cf147260
AD
491
492# b4_parse_param_vars
493# -------------------
494# Extra instance variables.
495m4_define([b4_parse_param_vars],
496 [m4_ifset([b4_parse_param],
e9690142 497 [
ac826bc4 498 // User arguments.
cf147260
AD
499b4_cc_var_decls(b4_parse_param)])])
500m4_define([b4_cc_var_decls],
e9690142 501 [m4_map_sep([b4_cc_var_decl], [
cf147260
AD
502], [$@])])
503m4_define([b4_cc_var_decl],
e9690142 504 [ $1;])
426903aa
AD
505
506
507## ---------##
508## Values. ##
509## ---------##
510
511# b4_yylloc_default_define
512# ------------------------
513# Define YYLLOC_DEFAULT.
514m4_define([b4_yylloc_default_define],
515[[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
516 If N is 0, then set CURRENT to the empty location which ends
517 the previous symbol: RHS[0] (always defined). */
518
519# ifndef YYLLOC_DEFAULT
520# define YYLLOC_DEFAULT(Current, Rhs, N) \
521 do \
522 if (N) \
523 { \
524 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
525 (Current).end = YYRHSLOC (Rhs, N).end; \
526 } \
527 else \
528 { \
529 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
530 } \
531 while (/*CONSTCOND*/ false)
532# endif
533]])
1dce289a
AD
534
535## -------- ##
536## Checks. ##
537## -------- ##
538
539b4_token_ctor_if([b4_variant_if([],
540 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
541 [cannot use '%s' without '%s'],
542 [%define api.token.constructor],
543 [%define api.value.type variant]))])])